package xin.marcher.module.evaluate.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.evaluate.domain.dto.ShopScoreWeightConfigurationResultDTO;
import xin.marcher.module.evaluate.domain.request.ShopScoreWeightConfigurationRequest;
import xin.marcher.module.evaluate.repository.EvaluateRepository;
import xin.marcher.module.evaluate.service.ShopScoreWeightConfigurationService;

import java.util.Objects;

import static xin.marcher.module.common.constants.evaluate.EvaluateConstants.TOTAL_WEIGHT;

/**
 *
 */
@Service
public class ShopScoreWeightConfigurationServiceImpl implements ShopScoreWeightConfigurationService {

    @Autowired
    private EvaluateRepository evaluateRepository;

    /**
     * 创建店铺评分权重
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopScoreWeightConfigurationResultDTO createShopScoreWeightConfiguration(ShopScoreWeightConfigurationRequest request) {
        // 参数校验
        checkParams(request);
        // 保存
        long id = evaluateRepository.createShopScoreWeightConfiguration(request);
        // 将其他信息修改为无效
        evaluateRepository.invalidShopScoreWeightConfiguration(id);
        return new ShopScoreWeightConfigurationResultDTO(Boolean.TRUE);
    }

    /**
     * 修改权重值
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopScoreWeightConfigurationResultDTO updateShopScoreWeightConfiguration(ShopScoreWeightConfigurationRequest request) {
        // 参数校验
        checkParams(request);
        // 修改权重值
        evaluateRepository.updateShopScoreWeightConfiguration(request);
        return new ShopScoreWeightConfigurationResultDTO(Boolean.TRUE);
    }

    /**
     * 创建店铺入参校验
     *
     * @param request
     */
    private void checkParams(ShopScoreWeightConfigurationRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getShopDescScoreWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getShopLogisticsScoreWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getShopServiceScoreWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        int sum = request.getShopDescScoreWeight() + request.getShopLogisticsScoreWeight() + request.getShopServiceScoreWeight();
        if (sum != TOTAL_WEIGHT) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

    }
}
